﻿using System;
using System.Linq;
using System.Text;
using Kaidou.Model;
using System.Data;
using System.Data.SqlClient;
using Kaidou.Util;
using System.Collections;

/**
 * 受注明細のパーシスター
 */
namespace Kaidou.Persister
{
    public class TRN_Juchu_DPersister
    {

        /**
	     * 受注明細を登録する
	     * @param con コネクション
	     * @param model 受注明細のデータモデル
	     * @return boolean
	     * @throws PersisterSQLException
	     */
	    public bool insert(SqlDB db, TrnJuchud M) 
        {
		     String sql =
			    "insert into trn_juchu_d ( "
				    + "juchu_no, "
				    + "juchu_rec_no, "
				    + "print_clss, "
				    + "seal_qty, "
				    + "trader_reg_no, "
				    + "seal_str_no, "
				    + "seal_end_no, "
				    + "reg_date, "
				    + "upd_date, "
				    + "upd_union_code, "
				    + "upd_branch_code, "
				    + "upd_func_id "
				    + ") values ( "
				    + "@juchu_no, "
				    + "@juchu_rec_no, "
				    + "@print_clss, "
				    + "@seal_qty, "
				    + "@trader_reg_no, "
				    + "@seal_str_no, "
				    + "@seal_end_no, "
				    + "@reg_date, "
				    + "@upd_date, "
				    + "@upd_union_code, "
				    + "@upd_branch_code, "
				    + "@upd_func_id)";

		    LogManager.info("TRN_Juchu_DPersister Insert sql : " + sql);
		    try {
			   
               // SqlDB db = new SqlDB();
                SqlParameter[] sqlParameter = {   
                                                new SqlParameter("@juchu_no",SqlDbType.VarChar,10),
                                                new SqlParameter("@juchu_rec_no",SqlDbType.SmallInt),
                                                new SqlParameter("@print_clss",SqlDbType.VarChar,1),
                                                new SqlParameter("@seal_qty",SqlDbType.Int),
                                                new SqlParameter("@trader_reg_no",SqlDbType.VarChar,8),
                                                new SqlParameter("@seal_str_no",SqlDbType.VarChar,9),
                                                new SqlParameter("@seal_end_no",SqlDbType.VarChar,9),
                                                new SqlParameter("@reg_date",SqlDbType.DateTime),
                                                new SqlParameter("@upd_date",SqlDbType.DateTime),
                                                new SqlParameter("@upd_union_code",SqlDbType.VarChar,5),
                                                new SqlParameter("@upd_branch_code",SqlDbType.SmallInt),
                                                new SqlParameter("@upd_func_id",SqlDbType.VarChar,20)
                                              };

                sqlParameter[0].Value = M.Id.JuchuNo;
                sqlParameter[1].Value = M.Id.JuchuRecNo;
                sqlParameter[2].Value = M.PrintClss;
                sqlParameter[3].Value = M.SealQty;
                if (StringUtil.isEmpty(M.TraderRegNo))
                {
                    sqlParameter[4].Value = "";
                }
                else
                {
                    sqlParameter[4].Value = M.TraderRegNo.Substring(0, 2) + "-" + M.TraderRegNo.Substring(2, 5);
                }
                sqlParameter[5].Value = M.SealStrNo == null ? "" : M.SealStrNo;
                sqlParameter[6].Value = M.SealEndNo == null ? "" : M.SealEndNo;
                sqlParameter[7].Value = M.RegDate;
                sqlParameter[8].Value = M.UpdDate;
                sqlParameter[9].Value = M.UpdUnionCode;
                sqlParameter[10].Value = M.UpdBranchCode;
                sqlParameter[11].Value = M.UpdFuncId==null?"":M.UpdFuncId;
                

                db.ExecuteCmd(sql, sqlParameter);
               // db.Close();

		    } catch (Exception e) {

			    LogManager.error("TRN_Juchu_DPersister Insert " + e.Message+e.Source+e.StackTrace);
                throw new Exception("TRN_Juchu_DPersister insert error");
                // return false;

		    }
            return true;

	    }
    
    
        /**
	    * 指定された受注No.のデータモデルを取得する。
	    * @param con コネクション
	    * @param juchu_no 受注No.
	    * @return TRN_Juchu_D[] データモデル
	    * @throws PersisterSQLException
	    */
	    public TrnJuchud[] getDetail(string juchu_no){

		    TrnJuchud model = null;
		    TrnJuchud[] ret = new TrnJuchud[5];

		    String sql =
			    "select "
				    + "juchu_no, "
				    + "juchu_rec_no, "
				    + "print_clss, "
				    + "seal_qty, "
				    + "trader_reg_no, "
				    + "seal_str_no, "
				    + "seal_end_no, "
				    + "reg_date, "
				    + "upd_date, "
				    + "upd_union_code, "
				    + "upd_branch_code, "
				    + "upd_func_id "
				    + "from trn_juchu_d "
				    + "where "
				    + "juchu_no = @juchu_no ";
            SqlDB db = new SqlDB();
            try
            {


                SqlParameter[] sqlParameter = { new SqlParameter("@juchu_no", SqlDbType.VarChar, 20) };
                sqlParameter[0].Value = juchu_no;
                SqlDataReader dataReader = db.GetDataCmd(sql, sqlParameter);

                int i = 0;
                while (dataReader.Read())
                {

                    model = new TrnJuchud();
                    TrnJuchudId id = new TrnJuchudId();
                    id.JuchuNo = (string)dataReader["juchu_no"];
                    id.JuchuRecNo = (short)dataReader["juchu_rec_no"];
                    model.Id = id;
                    model.PrintClss = (string)dataReader["print_clss"];
                    model.SealQty = (int)dataReader["seal_qty"];
                    model.TraderRegNo = (string)dataReader["trader_reg_no"];
                    model.TraderRegNo = model.TraderRegNo.Replace("-", ""); 
                    model.SealStrNo =(DBNull.Value == dataReader["seal_str_no"])?"": (string)dataReader["seal_str_no"];
                    model.SealEndNo = (DBNull.Value == dataReader["seal_end_no"]) ? "" : (string)dataReader["seal_end_no"];
                    model.RegDate = (DateTime)dataReader["reg_date"];
                    model.UpdDate = (DateTime)dataReader["upd_date"];
                    model.UpdUnionCode = (string)dataReader["upd_union_code"];
                    model.UpdBranchCode = (short)dataReader["upd_branch_code"];
                    model.UpdFuncId = (string)dataReader["Upd_func_id"];
                    ret[model.Id.JuchuRecNo - 1] = model;

                }


            }
            catch (Exception e)
            {
                LogManager.error("TRN_Juchu_DPersister get " + e.Message + e.StackTrace);

            }
            finally
            {
                db.Close();
            }

		    return ret;

	    }
    
    
        /**
	     * 受注明細を削除する
	     * @param con コネクション
	     * @param juchu_no 受注
	     * @param rec_no 受注明細桁番号
	     * @return boolean
	     * @throws PersisterSQLException
	     */
	    public void delete(SqlDB db, string juchu_no, int rec_no)  {

            String sql = "delete from trn_juchu_d where juchu_no = @juchu_no and juchu_rec_no = @juchu_rec_no ";
            LogManager.info("TRN_Juchu_DPersister delete sql : " + sql);
            try
            {

                SqlParameter[] sqlParameter = {   
                                                new SqlParameter("@juchu_no",SqlDbType.VarChar,10),
                                                new SqlParameter("@juchu_rec_no",SqlDbType.SmallInt)
                                               
                                              };

                sqlParameter[0].Value = juchu_no;
                sqlParameter[1].Value = rec_no;
                
                db.ExecuteCmd(sql, sqlParameter);
              

            }
            catch (Exception e)
            {

                LogManager.error("TRN_Juchu_DPersister delete " + e.Message);

            } 

	    }
    
        /**
	     * 受注明細を更新する
	     * @param con コネクション
	     * @param model 受注明細のデータモデル
	     * @return boolean
	     * @throws PersisterSQLException
	     */
	    public bool update(SqlDB db, TrnJuchud M) {
		    string sql =
			    "update trn_juchu_d set "
				    + "print_clss = @print_clss, "
				    + "seal_qty = @seal_qty, "
				    + "trader_reg_no = @trader_reg_no, "
				    + "seal_str_no = @seal_str_no, "
				    + "seal_end_no = @seal_end_no, "
				    + "upd_date = @upd_date, "
				    + "upd_union_code = @upd_union_code, "
				    + "upd_branch_code = @upd_branch_code, "
				    + "upd_func_id = @upd_func_id "
				    + "where "
				    + "juchu_no = @juchu_no"
				    + " and juchu_rec_no = @juchu_rec_no ";

            LogManager.info("TRN_Juchu_DPersister update sql : " + sql);
            try
            {

                SqlParameter[] sqlParameter = {   
                                                new SqlParameter("@juchu_no",SqlDbType.VarChar,10),
                                                new SqlParameter("@juchu_rec_no",SqlDbType.SmallInt),
                                                new SqlParameter("@print_clss",SqlDbType.VarChar,1),
                                                new SqlParameter("@seal_qty",SqlDbType.Int),
                                                new SqlParameter("@trader_reg_no",SqlDbType.VarChar,8),
                                                new SqlParameter("@seal_str_no",SqlDbType.VarChar,9),
                                                new SqlParameter("@seal_end_no",SqlDbType.VarChar,9),
                                               // new SqlParameter("@reg_date",SqlDbType.DateTime),
                                                new SqlParameter("@upd_date",SqlDbType.DateTime),
                                                new SqlParameter("@upd_union_code",SqlDbType.VarChar,5),
                                                new SqlParameter("@upd_branch_code",SqlDbType.SmallInt),
                                                new SqlParameter("@upd_func_id",SqlDbType.VarChar,20)
                                              };

                sqlParameter[0].Value = M.Id.JuchuNo;
                sqlParameter[1].Value = M.Id.JuchuRecNo;
                sqlParameter[2].Value = M.PrintClss;
                sqlParameter[3].Value = M.SealQty;
                LogManager.info("TRN_Juchu_DPersister update : JuchuNo " + M.Id.JuchuNo + " JuchuRecNo " + M.Id.JuchuRecNo + " SealQty "+M.SealQty);
                if (StringUtil.isEmpty(M.TraderRegNo))
                {
                    sqlParameter[4].Value = "";
                }
                else
                {

                    sqlParameter[4].Value = M.TraderRegNo.Substring(0, 2) + "-" + M.TraderRegNo.Substring(2, 5);
                }
                sqlParameter[5].Value = M.SealStrNo == null ? "" : M.SealStrNo;
                sqlParameter[6].Value = M.SealEndNo == null ? "" : M.SealEndNo;
               // sqlParameter[7].Value = M.RegDate;
                sqlParameter[7].Value = M.UpdDate;
                sqlParameter[8].Value = M.UpdUnionCode;
                sqlParameter[9].Value = M.UpdBranchCode;
                sqlParameter[10].Value = M.UpdFuncId == null ? "" : M.UpdFuncId;


                db.ExecuteCmd(sql, sqlParameter);
               // db.Close();

            }
            catch (Exception e)
            {

                LogManager.error("TRN_Juchu_DPersister update " + e.Message);
                return false;
            }

            return true;

	    }
    
    }
}
